package com.kele.basic;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author chunhui.li
 * @date 2022/1/22 15:38
 * @Description
 * @ClassName ThreadPoolTest
 */
public class ThreadPoolTest {
    static int data = 0;
    public static void main(String[] args) {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        List<Future<Integer>> list =new ArrayList<>();
        try {
            for (int i = 1; i <=5 ; i++) {
                Future<Integer> submit = fixedThreadPool.submit(ThreadPoolTest::add);
                list.add(submit);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fixedThreadPool.shutdown();
        }
        try {
            System.out.println(list.get(list.size()-1).get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

    }
    public synchronized static int add() {
        for (int i = 0; i < 2000; i++) {
            data++;
        }
//        System.out.println(Thread.currentThread().getName()+"===>"+data);
        return data;
    }
}
